from django import forms
from django.core.exceptions import ValidationError

from blog.models import Category


def validator_category_name(value):
    if Category.objects.filter(name=value).exists():
        raise ValidationError('类别名称不能重复')


class CategoryForm(forms.Form):
    name = forms.CharField(
        label='名称', max_length=32, validators=[validator_category_name])
    desc = forms.CharField(label='描述', required=False)


class CategoryModelForm(forms.ModelForm):
    class Meta:
        model = Category
        # fields = ['name', 'desc']
        exclude = []
